<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
	
	function __construct()
        {
		parent::__construct();
		$this->load->model('Users');
		$this->load->model('Administration');
	}

	function index()
	{
            /*
		$data['users']= $this->User->get_list();
		$this->load->view('layout/header') ; 
	        $this->load->view('user/user_list',$data);
		$this->load->view('layout/footer');
            */
	}
	
	function profile($id)
	{
            /*
		$data['user']= $this->User->get_data($id);
		$this->load->view('layout/header') ; 
	        $this->load->view('user/profile',$data);
		$this->load->view('layout/footer');
            */
	}
	
	function newuser()
	{
            
		  $this->form_validation->set_rules('username', 'Nombre', 'required');
		  $this->form_validation->set_rules('pass', 'Contrase&ntilde;a', 'required|matches[pass2]');
		  $this->form_validation->set_rules('pass2', 'Confirmar contrase&ntilde;a', 'required');
		  $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
		  
		  if($this->form_validation->run() == FALSE)
		  {
			 $this->load->view('layout/header') ; 
			 $this->load->view('user_new');
			 $this->load->view('layout/footer');
		  }
		  else
		  {	$var = $this->Users->insert($this->input->post());
			   if($var != false)
				    {
					     $userData = array('user'=>$var['email'],'pass'=>$var['password']);
					     $var = ($this->Users->ValidateSession($userData));
					     if($var != 0)
					     {
						      $this->session->set_userdata($var);
						      redirect("login/main"); 
					     }
					     else
					     {
						      redirect("/");    
					     }
				    }
				    else
				    {
					     $this->load->view('layout/header') ; 
					     $this->load->view('layout/menu');
					     $this->load->view('layout/footer');
				    }
		  }
            
	}
	
        function create()
        {
            $this->load->view('layout/header') ; 
            $this->load->view('user_new');
            $this->load->view('layout/footer');
        }
	
	function load_game()
	{
	     $data['title'] = "games";
	     $data['data']  = $this->Users->ReturnGame();
	     $data['enabled'] = $this->Users->date_limit();
	     $data['date'] = $this->Users->return_date();
	     $this->load->view('result_game/load_edit_game',$data);
	}
	
	function main_view()
	{
		$this->load->view('main_view');
	}
	
	function register_game()
	{
		if($this->Users->register_game($this->input->post())== false)
		{
			echo json_encode(0);
		}
		else
		{
			echo json_encode(1);
		}
			//redirect("login/main");
			/*
			$this->load->view('layout/header') ; 
			$this->load->view('layout/menu');
			$this->load->view('layout/footer');
			*/
		//}
	}
	
	function load_result()
	{
	     $data['title'] = "games";
	     $var = $this->Users->ReturnResult();
	     if($var == 0)
	     {
		//no hay resultados
		$data['results'] = 0;
	     }
	     else
	     {
		
		$data['results'] = $var;
	     }
	     
	     $data['data']  = $var;
	     $this->load->view('result_game/load_result',$data);
	}
	
	function load_profile()
	{
		$var = $this->Users->get_data($this->session->userdata('id_user'));
		$this->load->view('user/profile',$var);
	}
	
	public function update_profile()
        {
	    $resultado = $this->Users->update_profile($this->input->post());
            if($resultado == false)
		{
			echo json_encode(0);
		}
		else
		{
			echo json_encode($resultado);
		}
        }
	
	function date_of_user()
	{
	    $resultado = $this->Users->date_of_user($this->input->post());
            if($resultado == false)
		{
			echo json_encode(0);
		}
		else
		{
			echo json_encode($resultado);
		}
	}
	
	function date_of_user_results()
	{
	    $resultado = $this->Users->date_of_user_results($this->input->post());
            if($resultado == false)
		{
			echo json_encode(0);
		}
		else
		{
			echo json_encode($resultado);
		}
	}
	
	function positions()
	{
		$data['positions'] = $this->Administration->return_positions();
		$data['dates'] = $this->Administration->return_dates();
		$this->load->view('result_game/positions', $data);		
	}
	
	function result_for_date($idFecha)
	{
		$data['positions'] = $this->Administration->result_for_date($idFecha);
		$this->load->view('user/position_for_date',$data);		
	}
	
	function graphics()
	{
		$resultado = $this->Users->Estadistic_team($this->input->post());
		if($resultado == false)
		{
			echo json_encode(-1);
		}
		else
		{
			echo json_encode($resultado);
		}
	}
}
